package com.wk.paradise.main.controller;

import com.wk.paradise.main.bean.pojo.ContentPojo;
import com.wk.paradise.main.bean.pojo.Menu;
import com.wk.paradise.main.bean.pojo.ResultPojo;
import com.wk.paradise.main.config.menu.NavConfig;
import com.wk.paradise.redis.config.redisConfig.RedisPool;
import com.wk.paradise.redis.utils.ProtostuffSerializerUtil;
import com.wk.paradise.redis.utils.RedisListUtil;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Classname SyController
 * @Description TODO
 * @Date 2019/12/28 13:55
 * @Author by weikai
 */
@RestController
public class SyController {

  Logger logger = LoggerFactory.getLogger(SyController.class);

  @Autowired
  NavConfig navConfig;

  @Autowired
  RedisPool redisPool;

  @RequestMapping("/sy")
  public String sy(){
    return "html/sy.html";
  }
  /**
   * @return
   */
  @RequestMapping("/nav")
  @ResponseBody
  public Object menuList() {
    List<Menu> list = null;
    try {
      list = navConfig.getSubNav();
      return ResultPojo.OK_RESULT(list);
    } catch (Exception e) {
      logger.info("获取导航栏页面失败:{}", e.getMessage());
      return ResultPojo.ERROR_RESULT(list);
    }
  }

  /**
   * @return
   */
  @RequestMapping("/content")
  @ResponseBody
  public Object content() {
    Long llen = RedisListUtil.llen(redisPool,"content");
    List<String> list = RedisListUtil.rList(redisPool,"content",0,llen.intValue());
    List<ContentPojo> contentPojoList = new LinkedList<>();
    for(String contentStr:list){
      ContentPojo contentPojo = ProtostuffSerializerUtil.deserialize(new ContentPojo(),contentStr.getBytes());
      contentPojoList.add(contentPojo);
    }
    logger.info("得到的对象list为：{}",contentPojoList);
    try {
      return ResultPojo.OK_RESULT(contentPojoList);
    } catch (Exception e) {
      logger.info("加载内容出错:{}", e.getMessage());
      return ResultPojo.ERROR_RESULT(contentPojoList);
    }
  }

}
